#include "MiniDiscreteCurveHandler.h"

MiniDiscreteCurveHandler::MiniDiscreteCurveHandler(MiniObject *obj)
    : MiniHandler(obj)
{
    UpdateHandler();
}

float MiniDiscreteCurveHandler::CheckCollision(QPointF pos, QMatrix4x4 projection)
{
    // 之后增加选中程序
    Q_UNUSED(pos)
    Q_UNUSED(projection)
    return 1;
}

void MiniDiscreteCurveHandler::ChangeVertex(Vector3f dv)
{
    Q_UNUSED(dv)
}

void MiniDiscreteCurveHandler::UpdateHandler()
{
    InitHandler();

    // 转化为分段直线
    MiniDiscreteCurve *polyline = (MiniDiscreteCurve*)m_object;
    const MiniArray<Vector3f> &points = polyline->GetPoints();

    float* vertices = new float[points.size() * 3];
    unsigned int* indices = new unsigned int[points.size()];
    for (int i = 0; i < int(points.size()); i++)
    {
        indices[i] = i;
        vertices[i * 3 + 0] = points[i].x();
        vertices[i * 3 + 1] = points[i].y();
        vertices[i * 3 + 2] = points[i].z();
    }

    // 储存数据
    m_countIndices.push_back(points.size());
    m_countVertices.push_back(points.size());
    m_vertices.push_back(vertices);
    m_indices.push_back(indices);
    m_modes.push_back(GL_LINE_STRIP);
    m_color.push_back(m_basisColor);
    m_lineWidth.push_back(2);
}
